cc.Class({
    extends: cc.Component,

    properties: {
        prefabLevelLayer: cc.Prefab,
        pfLevelItem: cc.Prefab,

        listLevelLayer: cc.PageView,
        levelItemScrollView: cc.ScrollView,

        btnLeft: cc.Button,
        btnRight: cc.Button,

        labelCurPage: cc.Label,

        objPageNum: cc.Node,

        //大关对应小关背景颜色
        itemBgTypeByScene: null,

    },

    onLoad() {
        this.itemBgTypeByScene = new Map();
        //0 通用和绿色   1粉红色  2 黄色
        this.itemBgTypeByScene.set("A1", 0);
        this.itemBgTypeByScene.set("B1", 0);
        this.itemBgTypeByScene.set("C1", 1);
        this.itemBgTypeByScene.set("D1", 0);
        this.itemBgTypeByScene.set("E1", 0);
        this.itemBgTypeByScene.set("F1", 2);

    },
    start() {

        this.listLevelLayer.node.off(cc.Node.EventType.TOUCH_START, this.listLevelLayer._onTouchBegan, this.listLevelLayer, true);

        this.listLevelLayer.node.off(cc.Node.EventType.TOUCH_MOVE, this.listLevelLayer._onTouchMoved, this.listLevelLayer, true);

        this.listLevelLayer.node.off(cc.Node.EventType.TOUCH_END, this.listLevelLayer._onTouchEnded, this.listLevelLayer, true);

        this.listLevelLayer.node.off(cc.Node.EventType.TOUCH_CANCEL, this.listLevelLayer._onTouchCancelled, this.listLevelLayer, true);

    },
    createLevelLayer() {
        //大关数组
        var levelDataArr = window.getGameLevelDataConfig(0, true);
        var bigLevelNum = levelDataArr.length / 6;
        for (let index = 0; index < bigLevelNum; index++) {
            var gameSceneData = levelDataArr[(index + 1) * 6 - 1];
            this.createLevelLayerToScene(gameSceneData, index);
        }

    },

    createLevelLayerToScene(groupAreaDatas, index) {
        let objLevelLayer = cc.instantiate(this.prefabLevelLayer);
        let levelLayerCom = objLevelLayer.getComponent("LevelLayer");
        levelLayerCom.setLevelData(groupAreaDatas, index);
        levelLayerCom.nodeParent = this;
        if (index < 3) {
            this.listLevelLayer.content.getChildByName("page1").addChild(objLevelLayer);
        } else {
            this.listLevelLayer.content.getChildByName("page2").addChild(objLevelLayer);
        }
        // this.listLevelLayer.content.addChild(objLevelLayer);
    },


    closeLayer(event) {
        window.audioMgr.playGameAudio("buttonCheck")
        event.interactable = false;
        if (this.levelItemScrollView.node.active) {
            this.levelItemScrollView.content.removeAllChildren();
            this.levelItemScrollView.node.active = false;
            this.objPageNum.active = true;

            this.listLevelLayer.node.active = true;
            this.start();
        } else {
            window.closePop(this.node);
        }
    },

    showLevelContent: function (maxLevel) {
        this.listLevelLayer.node.active = false;
        this.levelItemScrollView.node.active = true;
        this.objPageNum.active = false;

        let minLevel = maxLevel - 6;
        for (let i = minLevel; i < maxLevel; i++) {
            var levelData = window.getGameLevelDataConfig(i + 1);
            let objLevelLayer = cc.instantiate(this.pfLevelItem);
            let levelLayerCom = objLevelLayer.getComponent("LevelItem");
            this.levelItemScrollView.content.addChild(objLevelLayer);
            levelLayerCom.setListItemInfo(levelData, i, this.itemBgTypeByScene);
        }
    },

    newChangePage(event) {
        window.audioMgr.playGameAudio("buttonCheck");
        let curPage = this.listLevelLayer.getCurrentPageIndex();
        let pageNum = null;
        if (event.target.name == "btnLeft") {
            pageNum = curPage - 1;
        } else {
            pageNum = curPage + 1;
        }
        this.btnLeft.interactable = pageNum >= 1;
        this.btnRight.interactable = pageNum <= 0;
        this.listLevelLayer.scrollToPage(pageNum);
        this.labelCurPage.string = pageNum + 1;
    }
});